/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAOInterface.DAOXMLConnection;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
 *
 * @author Fabian
 */
public class XMLConnection implements DAOXMLConnection {

    File fXmlFile;
    NodeList nList;
    Document doc;

    /**
     * 
     * @param nameXML
     * @return
     */
    @Override
    public boolean setConnection(String nameXML) {
        try {
            fXmlFile = new File("DataSource/"+nameXML);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            return true;

        } catch (Exception ex) {
           
            Logger.getLogger(XMLConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    /**
     * 
     * @return
     */
    @Override
    public NodeList getData() {
        nList = doc.getElementsByTagName("element");
        return nList;
    }

    /**
     * 
     * @param tag
     * @return
     */
    @Override
    public NodeList getDataBy(String tag) {
        nList = doc.getElementsByTagName(tag);
        return nList;
    }
}
